<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="700px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="150px">


          <el-form-item label="学校" prop="schoolId">
            <el-input
              v-if="false"
              v-model="form.schoolId"
              style="width: 370px;"
            />
            <div v-if="form.schoolId">
              {{ dialogTableDataSchoolId.showInputValue }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataSchoolId"
              @select-callback="dialogTableSelectedCallBackSchoolId"
            ></dialogTable>
          </el-form-item>

          <el-form-item label="校区" prop="campusId">
            <el-input
              v-if="false"
              v-model="form.campusId"
              style="width: 370px;"
            />
            <div v-if="form.campusId">
              {{ dialogTableDataCampusId.showInputValue }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataCampusId"
              @select-callback="dialogTableSelectedCallBackCampusId"
            ></dialogTable>
          </el-form-item>

          <el-form-item label="团队成员" prop="teamId">
            <el-input
              v-if="false"
              v-model="form.teamId"
              style="width: 370px;"
            />
            <div v-if="form.teamId">
              {{ dialogTableDataTeamId.showInputValue }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataTeamId"
              @select-callback="dialogTableSelectedCallBackTeamId"
            ></dialogTable>
          </el-form-item>
          <el-form-item label="角色" prop="roleId">
            <el-input
              v-if="false"
              v-model="form.roleId"
              style="width: 370px;"
            />
            <div v-if="form.roleId">
              {{ dialogTableDataRoleId.showInputValue }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataRoleId"
              @select-callback="dialogTableSelectedCallBackRoleId"
            ></dialogTable>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('schoolName')" prop="schoolName" label="学校" />
        <el-table-column v-if="columns.visible('campusName')" prop="campusName" label="校区" />
        <el-table-column v-if="columns.visible('teamUserName')" prop="teamUserName" label="团队成员名字" />
        <el-table-column v-if="columns.visible('roleName')" prop="roleName" label="角色名称" />
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','tanzerBaseMemberRole:edit','tanzerBaseMemberRole:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTanzerBaseMemberRole from '@/api/tanzer/tanzerBaseMemberRole'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";
import dialogTable from "@/views/components/dialogTable/index.vue";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '用户角色关联', url: 'api/tanzerBaseMemberRole', sort: 'id,desc', crudMethod: { ...crudTanzerBaseMemberRole }})
const defaultForm = { id: null, yxUserId: null, roleId: null, isDel: null, createTime: null, createBy: null, updateBy: null, updateTime: null, teamId: null, schoolId: null, campusId: null }
export default {
  name: 'TanzerBaseMemberRole',
  components: {dialogTable, pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {

      permission: {
        add: ['admin', 'tanzerBaseMemberRole:add'],
        edit: ['admin', 'tanzerBaseMemberRole:edit'],
        del: ['admin', 'tanzerBaseMemberRole:del']
      },
      rules: {
        yxUserId: [
          { required: true, message: '会员ID不能为空', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '角色ID不能为空', trigger: 'blur' }
        ],
        teamId: [
          { required: true, message: '会员ID不能为空', trigger: 'blur' }
        ],
        schoolId: [
          { required: true, message: '学校ID不能为空', trigger: 'blur' }
        ],
        campusId: [
          { required: true, message: '校区ID不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'yxUserId', display_name: '会员ID' },
        { key: 'roleId', display_name: '角色ID' },
        { key: 'teamId', display_name: '会员ID' },
        { key: 'schoolId', display_name: '学校ID' }
      ],


      dialogTableDataSchoolId: {
        title: "学校信息",
        dialogVisible: false,
        url: "api/tanzerBaseSchoolInfo",
        sort: "id,asc",
        buttonContext: "选择学校",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'schoolName', display_name: '学校名称' },
        ],
        elTtableColumns: [
          {
            columnProp: "schoolName",
            columnLabel: "学校名称"
          },
          {
            columnProp: "schoolPlace",
            columnLabel: "归属地"
          },
          {
            columnProp: "schoolType",
            columnLabel: "类型"
          },
          {
            columnProp: "schoolProperties",
            columnLabel: "级别"
          },
        ]
      },

      dialogTableDataCampusId: {
        title: "校区信息",
        dialogVisible: false,
        url: "api/tanzerBaseCampus",
        sort: "id,desc",
        buttonContext: "选择校区",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'schoolName', display_name: '学校名称' },
          { key: 'campusName', display_name: '校区名称' }
        ],
        elTtableColumns: [
          {
            columnProp: "schoolName",
            columnLabel: "学校名称"
          },
          {
            columnProp: "campusName",
            columnLabel: "校区名称"
          }
        ]
      },

      dialogTableDataTeamId: {
        title: "团队成员",
        dialogVisible: false,
        url: "api/tanzerBaseTeam",
        sort: "id,desc",
        buttonContext: "选择团队成员",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'name', display_name: '名称' },
          { key: 'studentPhone', display_name: '电话' },
          { key: 'status', display_name: '状态' }
        ],
        elTtableColumns: [
          {
            columnProp: "name",
            columnLabel: "名称"
          },
          {
            columnProp: "studentPhone",
            columnLabel: "电话"
          },
          {
            columnProp: "status",
            columnLabel: "状态"
          }
        ]
      },
      dialogTableDataRoleId: {
        title: "角色",
        dialogVisible: false,
        url: "api/tanzerBaseRole",
        sort: "id,desc",
        buttonContext: "选择团队角色",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'moduleName', display_name: '模块' },
          { key: 'roleName', display_name: '角色名称' },
        ],
        elTtableColumns: [
          {
            columnProp: "moduleName",
            columnLabel: "模块"
          },
          {
            columnProp: "roleName",
            columnLabel: "角色名称"
          }
        ]
      },

    }
  },
  watch: {
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.yxUserId
        delete this.crud.params.roleId
        delete this.crud.params.teamId
        delete this.crud.params.schoolId
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },

    //学校选择
    dialogTableSelectedCallBackSchoolId(row) {
      this.form.schoolId = row.id;
      if (this.form.schoolId) {
        this.dialogTableDataSchoolId.showInputValue = row.schoolName;
      }
    },

    //校区选择
    dialogTableSelectedCallBackCampusId(row) {
      this.form.campusId = row.id;
      if (this.form.campusId) {
        this.form.schoolId = row.id;
        this.form.schoolName = row.schoolName;
        this.dialogTableDataCampusId.showInputValue = row.campusName;
      }
    },

    //团队成员选择
    dialogTableSelectedCallBackTeamId(row) {
      this.form.teamId = row.id;
      if (this.form.teamId) {
        this.dialogTableDataTeamId.showInputValue = row.name;
      }
    },
    //角色选择
    dialogTableSelectedCallBackRoleId(row) {
      this.form.roleId = row.id;
      if (this.form.roleId) {
        this.dialogTableDataRoleId.showInputValue = row.roleName;
      }
    },
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
